<template>
  <el-card>
    <div class="character">
      <div class="top-form">
        <el-radio-group
          v-for="(item, index) in radioList"
          :key="index"
          v-model="radioValue"
          size="large"
          @change="handleChange(item)"
        >
          <el-radio-button :label="item.value">{{ item.label }}</el-radio-button>
        </el-radio-group>
        <el-input
          v-if="['1', '2', '3'].includes(radioValue)"
          v-model="inputSearch"
          style="max-width: 400px"
          placeholder="请输入检索内容"
          class="input-with-select"
          clearable
        >
          <template #prepend>
            <el-select v-model="selectValue" placeholder="请选择检索条件" style="width: 115px">
              <el-option label="知识库检索" value="1" />
            </el-select>
          </template>
          <template #append>
            <el-button type="primary" @click="handleSearch">搜索</el-button>
          </template>
        </el-input>
        <div class="search-time" v-if="['4'].includes(radioValue)">
          <el-select
            v-model="selectTimeValue"
            placeholder="Select"
            style="width: 115px; box-shadow: none"
          >
            <el-option label="时空检索" value="2" />
            <el-option label="起止日期" value="3" />
          </el-select>
          <el-input
            v-if="selectTimeValue === '2'"
            v-model="inputSearch"
            style="min-width: 370px"
            placeholder="时空检索"
          />
          <el-date-picker
            v-if="selectTimeValue === '3'"
            v-model="inputSearch"
            type="daterange"
            range-separator="To"
            start-placeholder="请输入起始时间"
            end-placeholder="请输入终止时间"
          />
          <el-button color="#f5f7fa" @click="handSearchTime" style="color: #909fc2">搜索</el-button>
        </div>
        <div class="search-time" v-if="['5'].includes(radioValue)">
          <el-select
            v-model="selectTimeValue"
            placeholder="Select"
            style="width: 115px; box-shadow: none"
          >
            <el-option label="时空检索" value="3" />
          </el-select>
          <el-date-picker
            v-if="selectTimeValue === '3'"
            v-model="inputSearch"
            type="daterange"
            range-separator="To"
            start-placeholder="请输入起始时间"
            end-placeholder="请输入终止时间"
          />
          <el-button color="#f5f7fa" @click="handSearchTime" style="color: #909fc2">搜索</el-button>
        </div>
      </div>
      <div
        v-if="['1'].includes(radioValue)"
        class="content-box"
        v-infinite-scroll="load"
        infinite-scroll-distance="10"
        :infinite-scroll-immediate="false"
      >
        <div class="card-box" v-for="(item, index) in tableModule.tableData" :key="index">
          <el-card>
            <div class="cb-content" @click="details(item)">
              <div class="cbc-top">
                <img :src="item.entityPhotoUrl || defaultImage" alt="暂无图片" title="人物图片" />
              </div>
              <div class="cbc-middle">
                <div class="cbt-name">{{ item.entityName }}</div>
              </div>
              <div class="cbc-bottom">
                <div class="cbt-content" v-html="item.entityIntro || `<p>暂无简介</p>`"></div>
              </div>
            </div>
          </el-card>
        </div>
      </div>
      <div v-if="['2'].includes(radioValue)" class="content-box">
        <el-empty v-if="JSON.stringify(tableModule.tableData) === '[]'" description="暂无数据" />
        <div class="card-box" v-for="(item, index) in tableModule.tableData" :key="index">
          <el-card>
            <div class="cb-content" @click="details(item)">
              <div class="cbc-middle">
                <div class="cbt-name">{{ item.eventName }}</div>
              </div>
              <div class="cbc-bottom" style="height: 15em">
                <div class="cbt-content" v-html="item.eventDetails || `<p>暂无描述</p>`"></div>
              </div>
            </div>
          </el-card>
        </div>
      </div>
      <div v-if="['3', '4'].includes(radioValue)" class="radio-group-table">
        <div style="height: 550px">
          <el-auto-resizer>
            <template #default="{ height, width }">
              <el-table-v2
                :key="keyRendering"
                :columns="columns"
                :data="tableModule.tableData"
                :width="width"
                :height="height"
                fixed
              />
            </template>
          </el-auto-resizer>
        </div>
        <el-pagination
          :current-page="tableModule.currentPage"
          :page-size="tableModule.pageSize"
          :page-sizes="[10, 20, 30, 50]"
          :small="false"
          :disabled="false"
          :background="false"
          layout="total, sizes, prev, pager, next, jumper"
          :total="tableModule.count"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
      <div v-if="['5'].includes(radioValue)" class="tabs-last">
        <retrieval ref="retrievalRef" :inputSearch="inputSearch"></retrieval>
      </div>
    </div>
    <detailsDialog ref="detailsDialogRef" :dialogTitle="dialogTitle" />
    <eventsDialog ref="eventsDialogRef" dialogTitle="关联事件" />
  </el-card>
</template>
<script setup lang="tsx">
import * as FormApi from '@/api/knowledge/character'
import { useRouter, useRoute } from 'vue-router'
import defaultImage from '@/assets/imgs/defaultImage.png'
import detailsDialog from './detailsDialog.vue'
import eventsDialog from './eventsDialog.vue'
import { Column } from 'element-plus'
import dayjs from 'dayjs'
import retrieval from './retrieval.vue'
const dialogTitle = ref('实体详情')
const retrievalRef = ref()
const detailsDialogRef = ref()
const eventsDialogRef = ref()
const route = useRoute()
const router = useRouter()
const radioValue = ref<string>('1')
const selectValue = ref<string>('1')
const selectTimeValue = ref<string>('3')
const radioList = reactive([
  { label: '人物图谱', value: '1' },
  { label: '事件图谱', value: '2' },
  { label: '地点图谱', value: '3' },
  // { label: '时间图谱', value: '4' },
  { label: '时空图谱', value: '5' }
])
onMounted(() => {
  getTableData('1', inputSearch.value)
})
const load = () => {
  tableModule.pageSize += 20
  console.log(tableModule.currentPage)
  getTableData(radioValue.value, inputSearch.value)
}
// 分页：pageSize 改变时会触发
const handleSizeChange = (val: number) => {
  tableModule.pageSize = val
  getTableData(radioValue.value, inputSearch.value)
}
// 分页：currentPage 改变时会触发
const handleCurrentChange = (val: number) => {
  tableModule.currentPage = val
  getTableData(radioValue.value, inputSearch.value)
}
const inputSearch = ref('')
// 表格数据
const tableModule = reactive({
  tableData: [], // 表格数据
  currentPage: 1, // 表格分页：当前页
  pageSize: 20, // 表格分页：每页显示条目个数
  count: 1 // 表格分页：总记录数
})
const keyRendering = ref(1)

const handleChange = (row) => {
  tableModule.currentPage = 1
  tableModule.tableData = []
  ++keyRendering.value
  columns.forEach((item) => {
    if (radioValue.value === '3')
      item.hidden = item.key === 'year' || item.key === 'month' || item.key === 'day' ? true : false
    else if (radioValue.value === '4') item.hidden = item.key === 'place' ? true : false
  })
  inputSearch.value = ''
  getTableData(row.value, inputSearch.value)
}
const getTableData = (entityType, entityName) => {
  console.log(entityType)
  console.log(entityName)
  if (entityType === '1') {
    const params = {
      graphId: route.query.graphId,
      releaseStatus: 1, // 发布状态：0未发布 1已发布
      entityType: entityType, // tabs状态：1 人物图谱 2 事件图谱 3 地点图谱 4 时间图谱
      entityName: entityName,
      pageNo: tableModule.currentPage,
      pageSize: tableModule.pageSize
    }
    FormApi.getGraphEntityPage(params).then((res) => {
      console.log(tableModule.tableData)
      // tableModule.tableData = []
      if (entityName) {
        tableModule.tableData = res.list
      } else {
        Object.assign(tableModule.tableData, res.list)
      }
    })
  } else if (entityType === '2') {
    const params = {
      graphId: route.query.graphId,
      releaseStatus: 1, // 发布状态：0未发布 1已发布
      entityType: entityType, // tabs状态：1 人物图谱 2 事件图谱 3 地点图谱 4 时间图谱
      eventName: entityName,
      pageNo: tableModule.currentPage,
      pageSize: tableModule.pageSize
    }
    FormApi.getGraphEventPage(params).then((res) => {
      Object.assign(tableModule.tableData, res.list)
    })
    console.log(tableModule.tableData)
  } else if (entityType === '3') {
    const params = {
      graphId: route.query.graphId,
      releaseStatus: 1, // 发布状态：0未发布 1已发布
      entityType: entityType, // tabs状态：1 人物图谱 2 事件图谱 3 地点图谱 4 时间图谱
      place: entityName,
      pageNo: tableModule.currentPage,
      pageSize: tableModule.pageSize
    }
    FormApi.getGraphPlacePage(params).then((res) => {
      Object.assign(tableModule.tableData, res.list)
    })
    console.log(tableModule.tableData)
  } else if (entityType === '4') {
    // tableModule.pageSize = 10
    const params = {
      graphId: route.query.graphId,
      releaseStatus: 1, // 发布状态：0未发布 1已发布
      entityType: entityType, // tabs状态：1 人物图谱 2 事件图谱 3 地点图谱 4 时间图谱
      pageNo: tableModule.currentPage,
      pageSize: tableModule.pageSize
    }
    if (entityName) {
      Object.assign(params, { queryTimeStart: dayjs(entityName[0]).format('YYYY/MM/DD') })
      Object.assign(params, { queryTimeEnd: dayjs(entityName[1]).format('YYYY/MM/DD') })
    }
    FormApi.getGraphTimePage(params).then((res) => {
      tableModule.tableData = []
      Object.assign(tableModule.tableData, res.list)
      tableModule.count = res.total
    })
    console.log(tableModule.tableData)
  }
}

const details = (item) => {
  console.log(item)
  console.log(radioValue.value)
  if (radioValue.value === '3' || radioValue.value === '4') detailsDialogRef.value.open(item)
  else if (radioValue.value === '1') {
    router.push({
      path: '/knowledge/character/atlas',
      query: {
        graphId: route.query.graphId,
        entityId: item.id,
        entityName: item.entityName,
        tabsType: radioValue.value
      }
    })
  } else if (radioValue.value === '2') {
    router.push({
      path: '/knowledge/character/atlas',
      query: {
        graphId: route.query.graphId,
        entityId: item.id,
        entityName: item.eventName,
        tabsType: radioValue.value
      }
    })
  }
}
const handleSearch = () => {
  if (selectValue.value === '1') getTableData(radioValue.value, inputSearch.value)
}
const handSearchTime = () => {
  retrievalRef.value.handleSearch()
}
const handleEvents = () => {
  eventsDialogRef.value.open()
}
const columns: Column<object>[] = [
  {
    key: 'place',
    title: '位置',
    dataKey: 'place',
    width: 150,
    align: 'center',
    cellRenderer: ({ cellData: place }) => <div>{place}</div>
  },
  {
    key: 'year',
    title: '年',
    dataKey: 'year',
    width: 100,
    align: 'center',
    cellRenderer: ({ cellData: year }) => <div>{year}</div>
  },
  {
    key: 'month',
    title: '月',
    dataKey: 'month',
    width: 100,
    align: 'center',
    cellRenderer: ({ cellData: month }) => <div>{month}</div>
  },
  {
    key: 'day',
    title: '日',
    dataKey: 'day',
    width: 100,
    align: 'center',
    cellRenderer: ({ cellData: day }) => <div>{day}</div>
  },
  {
    key: 'entityType',
    title: '实体类型',
    dataKey: 'entityType',
    width: 150,
    align: 'center',
    cellRenderer: ({ cellData: entityType }) => (
      <div>{entityType === 3 ? '地点图谱' : '时间图谱'}</div>
    )
  },
  {
    key: 'graphName',
    title: '图谱名称',
    dataKey: 'graphName',
    width: 150,
    align: 'center',
    cellRenderer: ({ cellData: graphName }) => <div>{graphName}</div>
  },
  {
    key: 'releaseStatus',
    title: '发布状态',
    dataKey: 'releaseStatus',
    width: 150,
    align: 'center',
    cellRenderer: ({ cellData: releaseStatus }) => (
      <>
        <el-tag v-show={Number(releaseStatus) === 0} type="info">
          未发布
        </el-tag>
        <el-tag v-show={Number(releaseStatus) === 1} type="primary">
          已发布
        </el-tag>
      </>
    )
  },
  {
    key: 'createTime',
    title: '创建时间',
    dataKey: 'createTime',
    width: 150,
    align: 'center',
    cellRenderer: ({ cellData: createTime }) => (
      <el-tooltip content={dayjs(createTime).format('YYYY/MM/DD')}>
        {<span class="flex items-center">{dayjs(createTime).format('YYYY/MM/DD')}</span>}
      </el-tooltip>
    )
  },
  {
    key: 'occurrenceTime',
    title: '发生时间',
    dataKey: 'occurrenceTime',
    width: 150,
    align: 'center',
    cellRenderer: ({ cellData: occurrenceTime }) => (
      <el-tooltip content={dayjs(occurrenceTime).format('YYYY/MM/DD')}>
        {<span class="flex items-center">{dayjs(occurrenceTime).format('YYYY/MM/DD')}</span>}
      </el-tooltip>
    )
  },
  {
    key: '操作',
    title: '操作',
    cellRenderer: (data) => (
      <>
        <el-button onClick={handleEvents.bind(this, data, 'edit')} type="success" size="small">
          关联事件
        </el-button>
      </>
    ),
    width: 150,
    align: 'center'
  }
]
</script>

<style scoped lang="scss">
.card-box {
  width: 25%;
  padding-right: 20px;
  padding-bottom: 20px;
  box-sizing: border-box;
}

.card-box:nth-child(4n) {
  padding-right: 0 !important;
}

.top-form {
  width: 100%;
  display: flex;
  justify-content: flex-start;
  align-items: center;
}

.el-input {
  margin-left: 100px;
}

.el-radio-group {
  .el-radio-button:nth-child(1) {
    border-left: 0;
  }

  .el-radio-button {
    margin-right: 20px;
  }

  .el-radio-button {
    border-left: var(--el-border);
  }

  .el-input {
    margin-top: 7px;
  }
}

.content-box {
  height: 615px;
  overflow: scroll;
  /* 隐藏默认的滚动条样式 */
  scrollbar-width: none; // Firefox
  -ms-overflow-style: none; // IE and Edge
  width: 100%;
  margin-top: 20px;
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-start;
  align-content: flex-start;

  .el-card {
    box-sizing: border-box;
  }

  ::v-deep(.el-card__body) {
    height: 300px;
    box-sizing: border-box;

    padding: 15px;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    align-items: center;
  }

  .cbt-box {
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 10;
    overflow: hidden;
  }

  .cb-content {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    width: 100%;

    .cbc-top {
      img {
        width: 100px;
        min-width: 100px;
        height: 120px;
      }
    }

    .cbc-middle {
      font-size: 25px;
      font-weight: 900;
      color: #db2126;
    }

    .cbc-bottom {
      text-align: center;
      width: 100%;
      border: 1px solid #c1c1c1;
      font-size: 16px;
      height: 7.5em;
      overflow-y: auto;
      scrollbar-width: thin;
    }
  }
}

.el-empty {
  width: 100%;
  height: 100%;
}

.search-time {
  margin-left: 100px;
  display: flex;
  justify-content: center;
  align-items: center;

  .el-select {
    ::v-deep(.select-trigger) {
      width: 115px !important;
    }

    ::v-deep(.el-input__wrapper) {
      border-radius: 4px 0 0 4px !important;
      background-color: #f5f7fa;
    }
  }

  .el-input {
    margin-left: 0;

    ::v-deep(.el-input__wrapper) {
      // border-radius: 0 4px 4px 0 !important;
      height: 30px !important;

      border-radius: 0 !important;
    }
  }

  ::v-deep(.el-date-editor) {
    // border-radius: 0 4px 4px 0 !important;
    border-radius: 0 !important;
  }

  ::v-deep(.el-button) {
    border: 1px solid #dcdfe6;
    border-left: none;
    border-radius: 0 4px 4px 0 !important;
    // border-radius: 0 !important;
  }
}

.tabs-last {
  margin-top: 20px;
}
.radio-group-table {
  // border: 1px solid red;
  margin-top: 20px;
}
</style>
